home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.4)
-
- import getopt
- from getopt import GetoptError
- from test.test_support import verify, verbose, run_doctest
- import os
-
- def expectException(teststr, expected, failure = AssertionError):
- '''Executes a statement passed in teststr, and raises an exception
- (failure) if the expected exception is *not* raised.'''
-
- try:
- exec teststr
- except expected:
- pass
-
- raise failure
-
- old_posixly_correct = os.environ.get('POSIXLY_CORRECT')
- if old_posixly_correct is not None:
- del os.environ['POSIXLY_CORRECT']
-
- if verbose:
- print 'Running tests on getopt.short_has_arg'
-
- verify(getopt.short_has_arg('a', 'a:'))
- verify(not getopt.short_has_arg('a', 'a'))
- expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
- expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
- if verbose:
- print 'Running tests on getopt.long_has_args'
-
- (has_arg, option) = getopt.long_has_args('abc', [
- 'abc='])
- verify(has_arg)
- verify(option == 'abc')
- (has_arg, option) = getopt.long_has_args('abc', [
- 'abc'])
- verify(not has_arg)
- verify(option == 'abc')
- (has_arg, option) = getopt.long_has_args('abc', [
- 'abcd'])
- verify(not has_arg)
- verify(option == 'abcd')
- expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", GetoptError)
- expectException("has_arg, option = getopt.long_has_args('abc', [])", GetoptError)
- expectException('has_arg, option = ' + "getopt.long_has_args('abc', ['abcd','abcde'])", GetoptError)
- if verbose:
- print 'Running tests on getopt.do_shorts'
-
- (opts, args) = getopt.do_shorts([], 'a', 'a', [])
- verify(opts == [
- ('-a', '')])
- verify(args == [])
- (opts, args) = getopt.do_shorts([], 'a1', 'a:', [])
- verify(opts == [
- ('-a', '1')])
- verify(args == [])
- (opts, args) = getopt.do_shorts([], 'a', 'a:', [
- '1'])
- verify(opts == [
- ('-a', '1')])
- verify(args == [])
- (opts, args) = getopt.do_shorts([], 'a', 'a:', [
- '1',
- '2'])
- verify(opts == [
- ('-a', '1')])
- verify(args == [
- '2'])
- expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", GetoptError)
- expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", GetoptError)
- if verbose:
- print 'Running tests on getopt.do_longs'
-
- (opts, args) = getopt.do_longs([], 'abc', [
- 'abc'], [])
- verify(opts == [
- ('--abc', '')])
- verify(args == [])
- (opts, args) = getopt.do_longs([], 'abc=1', [
- 'abc='], [])
- verify(opts == [
- ('--abc', '1')])
- verify(args == [])
- (opts, args) = getopt.do_longs([], 'abc=1', [
- 'abcd='], [])
- verify(opts == [
- ('--abcd', '1')])
- verify(args == [])
- (opts, args) = getopt.do_longs([], 'abc', [
- 'ab',
- 'abc',
- 'abcd'], [])
- verify(opts == [
- ('--abc', '')])
- verify(args == [])
- (opts, args) = getopt.do_longs([], 'foo=42', [
- 'foo-bar',
- 'foo='], [])
- verify(opts == [
- ('--foo', '42')])
- verify(args == [])
- expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", GetoptError)
- expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", GetoptError)
- cmdline = [
- '-a',
- '1',
- '-b',
- '--alpha=2',
- '--beta',
- '-a',
- '3',
- '-a',
- '',
- '--beta',
- 'arg1',
- 'arg2']
- if verbose:
- print 'Running tests on getopt.getopt'
-
- (opts, args) = getopt.getopt(cmdline, 'a:b', [
- 'alpha=',
- 'beta'])
- verify(opts == [
- ('-a', '1'),
- ('-b', ''),
- ('--alpha', '2'),
- ('--beta', ''),
- ('-a', '3'),
- ('-a', ''),
- ('--beta', '')])
- verify(args == [
- 'arg1',
- 'arg2'])
- expectException("opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", GetoptError)
- if verbose:
- print 'Running tests on getopt.gnu_getopt'
-
- cmdline = [
- '-a',
- 'arg1',
- '-b',
- '1',
- '--alpha',
- '--beta=2']
- (opts, args) = getopt.gnu_getopt(cmdline, 'ab:', [
- 'alpha',
- 'beta='])
- verify(opts == [
- ('-a', ''),
- ('-b', '1'),
- ('--alpha', ''),
- ('--beta', '2')])
- verify(args == [
- 'arg1'])
- (opts, args) = getopt.gnu_getopt(cmdline, '+ab:', [
- 'alpha',
- 'beta='])
- verify(opts == [
- ('-a', '')])
- verify(args == [
- 'arg1',
- '-b',
- '1',
- '--alpha',
- '--beta=2'])
- os.environ['POSIXLY_CORRECT'] = '1'
- (opts, args) = getopt.gnu_getopt(cmdline, 'ab:', [
- 'alpha',
- 'beta='])
- verify(opts == [
- ('-a', '')])
- verify(args == [
- 'arg1',
- '-b',
- '1',
- '--alpha',
- '--beta=2'])
- if old_posixly_correct is None:
- del os.environ['POSIXLY_CORRECT']
- else:
- os.environ['POSIXLY_CORRECT'] = old_posixly_correct
- libreftest = "\nExamples from the Library Reference: Doc/lib/libgetopt.tex\n\nAn example using only Unix style options:\n\n\n>>> import getopt\n>>> args = '-a -b -cfoo -d bar a1 a2'.split()\n>>> args\n['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']\n>>> optlist, args = getopt.getopt(args, 'abc:d:')\n>>> optlist\n[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]\n>>> args\n['a1', 'a2']\n\nUsing long option names is equally easy:\n\n\n>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'\n>>> args = s.split()\n>>> args\n['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']\n>>> optlist, args = getopt.getopt(args, 'x', [\n... 'condition=', 'output-file=', 'testing'])\n>>> optlist\n[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]\n>>> args\n['a1', 'a2']\n\n"
- __test__ = {
- 'libreftest': libreftest }
- import sys
- run_doctest(sys.modules[__name__], verbose)
- if verbose:
- print 'Module getopt: tests completed successfully.'
-
-